package com.nf.controller.home;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.ShowDateVoDao;
import com.nf.dao.ShowInfoDao;
import com.nf.dao.impl.ShowDateVoDaoImpl;
import com.nf.dao.impl.ShowInfoDaoImpl;
import com.nf.entity.ShowInfo;
import com.nf.vo.ShowDateVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 编写servlet，实现根据影片ID 查询出所有的放映计划，并以二维数组的json格式返回
 */
@WebServlet("/api/SelectShowsByFilmIdServlet.do")
public class SelectShowsByFilmIdServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id= Integer.parseInt(req.getParameter("filmId"));

        ShowDateVoDao dao1=new ShowDateVoDaoImpl();
        ShowInfoDao dao2=new ShowInfoDaoImpl();

        List<ShowDateVo> list1=dao1.selectByFilmId(id);

        ShowInfo[][] ss=new ShowInfo[list1.size()][];

        for(int i=0;i<list1.size();i++){
            List<ShowInfo> list2=dao2.selectByShowDate(list1.get(i).getShowDate(),id);
            ss[i]=new ShowInfo[list2.size()];

            for(int j=0;j<list2.size();j++){
                ss[i][j]=list2.get(j);
            }
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        out.print(new Gson().toJson(successJson(ss)));

        out.flush();
        out.close();

    }
}
